package com.taomu.app.zongchouhuli.utils.dialog;


import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.taomu.app.zongchouhuli.R;
import com.taomu.app.zongchouhuli.utils.DpUtils;
import com.taomu.app.zongchouhuli.weight.PasswordText;

public class PayPasswordDialog extends Dialog implements View.OnLayoutChangeListener{

    private TextView mPayWayTv;

    private TextView mPriceTv;

    private PasswordText mPasswordTv;

    private ImageView mCloseIm;

    private long money = -1l;

    private String payway;

    public static PayPasswordDialog getInstence(Context context){
        PayPasswordDialog dialog = new PayPasswordDialog(context);
        return dialog;
    }

    public static PayPasswordDialog getInstence(Context context,String payway, long money){
        PayPasswordDialog dialog = new PayPasswordDialog(context);
        dialog.payway = payway;
        dialog.money = money;
        return dialog;
    }

    public PayPasswordDialog(Context context) {
        super(context);
    }

    public PayPasswordDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected PayPasswordDialog(Context context, boolean cancelable, DialogInterface.OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
        setContentView(R.layout.dialog_pwd_view);
        initView();
        initEvent();
    }

    @Override
    public void dismiss() {
        switchInputKeyboard(false);
        super.dismiss();
    }

    private String content;

    public void setContent(String content){
        this.content = content;
    }

    private void initView(){
        mPayWayTv = findViewById(R.id.pay_pwd_dialog_select_tv);
        mPriceTv = findViewById(R.id.pay_pwd_dialog_money_tv);
        if(!TextUtils.isEmpty(content)) {
            mPriceTv.setText(content);
        }
        mPasswordTv = findViewById(R.id.gpv_new_psd);
        mCloseIm = findViewById(R.id.btn_close);
        if(!TextUtils.isEmpty(payway)){
            mPayWayTv.setText(payway);
        }
        if(money != -1){
            mPriceTv.setText(String.valueOf(money)+"元");
        }
    }

    private void initEvent(){
        getWindow().getDecorView().postDelayed(new Runnable() {
            @Override
            public void run() {
                mPasswordTv.getEditText().setFocusable(true);
                mPasswordTv.getEditText().setFocusableInTouchMode(true);
                mPasswordTv.getEditText().requestFocus();
                InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(mPasswordTv.getEditText(), InputMethodManager.SHOW_IMPLICIT);
                switchInputKeyboard(true);
            }
        }, 200);
        mCloseIm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switchInputKeyboard(false);
                dismiss();
            }
        });
        mPasswordTv.setPasswordTextListener(new PasswordText.PasswordTextListener() {
            @Override
            public void onComplete(String pwd) {
                if(listener != null){
                    listener.onFinishInput(pwd);
                }
                dismiss();
            }
        });
    }

    /**
     * 切换输入键盘状态
     *
     * @param show true：显示键盘，false：隐藏键盘
     */
    private void switchInputKeyboard(boolean show) {
        InputMethodManager imm = (InputMethodManager) getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        if (show) {
            imm.showSoftInput(mPasswordTv.getEditText(), 0);
        } else {
            imm.hideSoftInputFromWindow(mPasswordTv.getEditText().getWindowToken(), 0);
        }
    }

    @Override
    public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
        if(oldBottom != 0 && bottom != 0 &&(oldBottom - bottom > (DpUtils.screenHeight()/3))){


        }else if(oldBottom != 0 && bottom != 0 &&(bottom - oldBottom > (DpUtils.screenHeight()/3))){
            dismiss();
        }
    }

    private PayPasswordListener listener;

    public void setPayPasswordListener(PayPasswordListener listener){
        this.listener = listener;
    }

    public interface PayPasswordListener{
        void onFinishInput(String pwd);
    }
}
